package playing.action;

import playing.PlantLogin;
import playing.PlayerFriendships;
import playing.showfrends;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

public class friends_add_or_delete extends JFrame {
    private int userId;

    public friends_add_or_delete(int id) {
        this.userId = id;

        // Set up the frame
        setTitle("好友管理");
        setSize(400, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        // Create components
        JButton queryButton = new JButton("查询所有用户");
        JButton addButton = new JButton("添加好友");
        JButton deleteButton = new JButton("删除好友");
        JButton backButton = new JButton("返回好友界面");

        // Add action listeners to buttons
        queryButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                List<Integer> ids = new PlantLogin().getAllUserIds();
                new showfrends().printAllUsersByIdList(ids);
            }
        });

        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String input = JOptionPane.showInputDialog("请输入你想添加好友的id");
                int friendId = Integer.parseInt(input);
                if (new PlantLogin().userExistsById(friendId)) {
                    new PlayerFriendships().addFriend(userId, friendId);
                    new PlayerFriendships().addFriend(friendId, userId);
                    JOptionPane.showMessageDialog(null, "好友添加成功");
                } else {
                    JOptionPane.showMessageDialog(null, "失败");
                }
            }
        });

        deleteButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String input = JOptionPane.showInputDialog("请输入你想删除的好友的id");
                int friendId = Integer.parseInt(input);
                if (new PlayerFriendships().isFriend(userId, friendId)) {
                    new PlayerFriendships().deleteFriend(userId, friendId);
                    new PlayerFriendships().deleteFriend(friendId, userId);
                    JOptionPane.showMessageDialog(null, "好友删除成功");
                } else {
                    JOptionPane.showMessageDialog(null, "失败");
                }
            }
        });

        backButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // Implement the action to go back to friend interface
                // new friends(userId);
                // Note: friends class is not provided, so it's commented out.
                dispose(); // close the current window
            }
        });

        // Layout setup
        JPanel panel = new JPanel();
        panel.add(queryButton);
        panel.add(addButton);
        panel.add(deleteButton);
        panel.add(backButton);

        add(panel);
        setVisible(true);
    }

    public static void main(String[] args) {
        // Example usage
        new friends_add_or_delete(1);
    }
}
